﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading;

using VDataAdapter;
using VDataAdapter.Communication;
using VDataAdapter.BlockingQueue;
using VDataAdapter.EventManagement;


namespace VDataAdapter.TestPreparation
{
    [TestClass]
    public class DataSourcePreparation
    {
        private FirstTimeEvent[] FindFirstTimeEvent(Event[] list)
        {
            var result = new List<FirstTimeEvent>();
            
            foreach (Event ev in list)
            {
                if (ev.GetType().Equals(typeof(FirstTimeEvent)))
                    result.Add((FirstTimeEvent)ev);        
            }

            return result.ToArray();
        }

        /// <summary>
        /// Update DataSource using basePrices: all stock symbols are updated
        /// </summary>
        /// <param name="basePrices"></param>
        /// <param name="dataSource"></param>
        private void UpdateDataSource(IDictionary<string, PriceLibrary.PriceService.StockBase> basePrices, DataSource dataSource)
        {
            PriceLibrary.PriceService.StockInfoStatic infoStatic;

            foreach (KeyValuePair<string, PriceLibrary.PriceService.StockBase> p in basePrices)
            {
                 infoStatic =
                    (PriceLibrary.PriceService.StockInfoStatic)p.Value;
                dataSource.UpdateStockBase(infoStatic);
            }
        }

        private FirstTimeEvent[] LoadFirstTimeEvent(string path)
        {
            throw new NotImplementedException();            
            Event[] list = EventManager.LoadEvent(path);
            return FindFirstTimeEvent(list);
        }

        /// <summary>
        /// Because I forgot to commit the datasource.test, I'll have to call the event and write baseprices.test separately in order to test TransactionMessage
        /// </summary>
        [TestMethod(), Ignore()]
        public void WriteDataSourceDataUsingPSAdapter()
        {
            string testDataFolder = TestDataFolder.GetTestDataFolder();

            var firstEvent = LoadFirstTimeEvent(testDataFolder + "\\" + "events.test");
            
            DataSource dataSource = new DataSource();

            // filter FirstTimeEvent list in a stupid way

            foreach (FirstTimeEvent ev in firstEvent)
            {
                if (ev.Baseprices.Count != 0)
                {
                    UpdateDataSource(ev.Baseprices, dataSource);
                }
            }

            var writer = new PriceServerObjectFile();
            writer.WriteToFile(testDataFolder + "\\" + "basePrices.test", new Object[] { dataSource.BasePrices });

            // PSAdapterThread psAdapter = new PSAdapterThread(new System.Threading.ManualResetEvent(
        }
    
        
    }
}
